OSX OpenCV for Python2.7/Python3.5 环境配置

OpenCV 的环境配置。讲怎样用 conda 为 python 2.7 和 python 3.5 安装 opencv。

Setup python3

安装Anaconda Python 3,因为它自动配置了很多 dependencies,方便我们之后使用。按要求安装好后记得重新开一个 terminal 然后输入 python 验证版本。

1
2
3
4
5
MacBook-Pro-2:~ Shuang$ python
Python 3.6.0 |Anaconda 4.3.0 (x86_64)| (default, Dec 23 2016, 13:19:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Install OpenCV

1
2
3
4
5
6
7
8
9
10
11
12
13
$ pip install pillow
...
Successfully installed olefile-0.44
$ conda install -c menpo opencv3=3.1.0
Fetching package metadata ...........
Solving package specifications: .
UnsatisfiableError: The following specifications were found to be in conflict:
- opencv3 3.1.0* -> python 2.7* -> openssl 1.0.1*
- python 3.6*
Use "conda info <package>" to see the dependencies for each package.

遇到版本不兼容的问题,尝试了各种各样的方法都不行,最后猜着可能是 python 3.6 版本太高了?于是决定尝试一下 python 3.5,结果居然成功了。。。

1
2
3
$ conda create -n python3.5 python=3.5
$ source activate python3.5
$ conda install -c menpo opencv3=3.1.0

记得每次都要 source 一下。

测试一下:

1
2
3
4
5
6
$ python3.5
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> exit()

如法炮制,python 2.7 也成功安装了。

1
2
3
4
5
6
7
8
9
10
$ conda create -n python2.7 python=2.7
$ source activate python2.7
$ python2.7
Python 2.7.13 |Anaconda 4.3.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>> exit()

Install moviepy

安装 moviepy 来处理 video,以后要用。

1
$ pip install moviepy

Jupyter Notebook

在 python3 的环境下可以通过 jupyter notebook 来进行交互,运行部分代码并查看结果。

1
$ jupyter notebook

如何让 jupyter notebook 也能运行 python 2 呢?
尝试了网上的方法

1
2
python2 -m pip install --upgrade ipykernel # install the kernel package for Python 2
python2 -m ipykernel install # register the Python 2 kernelspec

然而总是报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 317, in run
prefix=options.prefix_path,
File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 742, in install
**kwargs
File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 831, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 1032, in move_wheel_files
isolated=self.isolated,
File "/Library/Python/2.7/site-packages/pip/wheel.py", line 247, in move_wheel_files
prefix=prefix,
File "/Library/Python/2.7/site-packages/pip/locations.py", line 153, in distutils_scheme
i.finalize_options()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 346, in finalize_options
self.create_home_path()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 565, in create_home_path
os.makedirs(path, 0700)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 150, in makedirs
makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Users/sure/Library/Python/2.7'
You are using pip version 7.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

发现下面这种方法可以成功

1
2
3
conda create -n py2 python=2 anaconda
source activate py2
ipython kernel install

徐阿衡 wechat
欢迎关注:徐阿衡的微信公众号
客官,打个赏呗~